<script setup lang="ts">
import type { BannerItem, CategoryItem, HotItem } from '@/types/home';
import CustomNavbar from './components/CustomNavbar.vue'
import CategoryPanel from './components/CategoryPanel.vue'
import HotPanel from './components/HotPanel.vue'
import PageSkeleton from './components/PageSkeleton.vue'
import { ref,onMounted } from 'vue';
import { getHomeBannerAPI,getHomeCategoryAPI,getHomeHotAPI } from '@/services/home';
import { useGuessList } from '@/composables';
// 获取轮播图数据
const bannerList = ref<BannerItem[]>([])
const getHomeBannerData = async () => {
  const res = await getHomeBannerAPI()
  bannerList.value = res.result
}
// 获取分类面板数据
const categoryList = ref<CategoryItem[]>([])
const getHomeCategoryData = async () => {
  const res = await getHomeCategoryAPI()
  categoryList.value = res.result
}

// 热门推荐数据
const hotList = ref<HotItem[]>([])
  const getHomeHotAPIData = async () => {
  const res = await getHomeHotAPI()
  hotList.value = res.result
}
const isLoading=ref(false)
onMounted(async ()=>{
  await Promise.allSettled([getHomeBannerData(),getHomeCategoryData(),getHomeHotAPIData()])
  isLoading.value=true
})

const { guessRef, onScrolltolower } = useGuessList()

const isTriggered=ref(false)
const onRefresherrefresh=async ()=>{
  isTriggered.value=true
  guessRef.value?.resetData()
  await Promise.all([getHomeBannerData(),getHomeCategoryData(),getHomeHotAPIData(), guessRef.value?.getMore()])
  isTriggered.value=false
}
</script>

<template>
  <!-- 自定义导航栏 -->
  <CustomNavbar />
  <scroll-view @refresherrefresh="onRefresherrefresh" :refresher-triggered="isTriggered" refresher-enabled @scrolltolower="onScrolltolower" class="scroll-view" scroll-y>

    <template v-if="isLoading">
      <!-- 自定义轮播图 -->
      <XtxSwiper :list="bannerList" />
      <!-- 分类面板 -->
      <CategoryPanel :list="categoryList" />
      <!-- 热门推荐 -->
      <HotPanel :list="hotList" />
      <!-- 猜你喜欢 -->
      <XtxGuess ref="guessRef" />
    </template>
    <PageSkeleton v-else />
  </scroll-view>
</template>

<style lang="scss">
/* #ifdef APP-PLUS */
#app,
/* #endif */
page{
  background-color: #f7f7f7;
  display: flex;
  flex-direction: column;
  height: 100%;
}
.scroll-view{
  flex:1
}
</style>
